package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.ActivityRegisterLogic;
import cn.topevery.garden.dal.util.FileUtils;
import cn.topevery.garden.entity.dto.ActivityRegisterDTO;
import cn.topevery.garden.entity.po.ActivityRegister;
import cn.topevery.garden.entity.po.GreFile;
import cn.topevery.garden.entity.query.ActivityRegisterQuery;
import cn.topevery.garden.entity.vo.ActivityRegisterVO;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 公园大型活动登记
 *
 * @author code-generate
 * @date 2022-10-28
 */
@Api(tags = "公园大型活动登记")
@RestController
@RequestMapping("/activityRegister")
public class ActivityRegisterController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    ActivityRegisterLogic activityRegisterLogic;

    @Autowired
    private FileUtils fileUtils;

    /**
     * 获取公园大型活动登记分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取公园大型活动登记页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<ActivityRegisterVO>> listPage(@RequestBody ActivityRegisterQuery query) {
        DataPager<ActivityRegisterVO> page = activityRegisterLogic.listPage(query);
        page.records.forEach(vo -> vo.setFileList(fileUtils.getFile(vo.getId(), GreFile.TYPE_ACTIVITY_REGISTER)));
        return JsonResult.ok(page);
    }

    @ApiOperation("新增/编辑 公园大型活动登记信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody ActivityRegisterDTO dto) {
        ActivityRegister activityRegister = mapper.map(dto, ActivityRegister.class);
        String userId = getUser().id.toString();
        activityRegisterLogic.saveOrUpdate(userId, activityRegister);
        fileUtils.saveFile(dto.getFileList(), activityRegister.getId(), userId, GreFile.TYPE_ACTIVITY_REGISTER);
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<ActivityRegisterVO> getInfoById(@RequestParam Integer id) {
        ActivityRegister entity = activityRegisterLogic.getEntity(id);
        ActivityRegisterVO vo= mapper.map(entity, ActivityRegisterVO.class);
        vo.setFileList(fileUtils.getFile(vo.getId(), GreFile.TYPE_ACTIVITY_REGISTER));
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        activityRegisterLogic.deleteBatchById(getUser().id.toString(),ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出公园大型活动登记信息")
    @GetMapping("/export")
    public void export(ActivityRegisterQuery query, HttpServletResponse response) {
        List<ActivityRegisterVO> list = activityRegisterLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "公园大型活动登记", "sheet1", ActivityRegisterVO.class, "公园大型活动登记", response);
    }

    @ApiOperation("公园活动统计")
    @PostMapping("/countActivities")
    public JsonResult<?> countActivities(@RequestBody ActivityRegisterQuery query) {
        return activityRegisterLogic.countActivities(query);
    }
}
